/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.expert.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.utils.CacheUtils;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.expert.entity.Product;
import com.chlitina.store.modules.expert.entity.SolutionFaceAndProduct;
import com.chlitina.store.modules.expert.entity.SolutionFaceProductMix;
import com.chlitina.store.modules.expert.entity.SolutionFaceProductStep;
import com.chlitina.store.modules.expert.service.SolutionFaceProductMixService;
import com.chlitina.store.modules.expert.service.SolutionFaceProductStepService;
import com.chlitina.store.modules.expert.utils.ExpertUtils;
import com.google.common.collect.Maps;

/**
 *  方案产品步骤Controller
 * @author liujiang
 * @version 2013-08-19
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/expert/solutionFaceProductMix")
public class SolutionFaceProductMixController extends BaseController {

	@Autowired
	private SolutionFaceProductMixService solutionFaceProductMixService;
	@Autowired
	private SolutionFaceProductStepService solutionFaceProductStepService;
	
	@ModelAttribute
	public SolutionFaceProductMix get(@RequestParam(required=false) Long id) {
		if (id != null){
			return solutionFaceProductMixService.get(id);
		}else{
			return new SolutionFaceProductMix();
		}
	}
	
	private String getSelectPage(Page page, Long stepId){
		Long select_mixId = (Long) CacheUtils.get("select_mixId");
		String currentId = "";
		if(select_mixId != null) {
	        int index = solutionFaceProductMixService.getIndexById(select_mixId, stepId);
	        ExpertUtils.setPageNoByCurrentId(page, index);
	        currentId = String.valueOf(select_mixId);
	        CacheUtils.remove("select_mixId");
		}
		return currentId;
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(Long stepId, SolutionFaceProductMix solutionFaceProductMix, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SolutionFaceProductMix> page = new Page<SolutionFaceProductMix>(request, response);
		String currentId = getSelectPage(page, stepId);
		Page<SolutionFaceProductMix> current_page = solutionFaceProductMixService.find(page, solutionFaceProductMix, stepId); 
        model.addAttribute("page", current_page);
        model.addAttribute("currentId", currentId);
        model.addAttribute("stepId", stepId);
		return "modules/expert/solutionFaceProductMixList";
	}

	@RequestMapping(value = "form")
	public String form(Long stepId, SolutionFaceProductMix solutionFaceProductMix, Model model) {
		CacheUtils.put("select_stepId", stepId);
		Map<Long, String> productMap = Maps.newHashMap();
		if(stepId != null){
			SolutionFaceProductStep step = solutionFaceProductStepService.get(stepId);
			List<SolutionFaceAndProduct> list = step.getSolutionFace().getProductList();
			for (SolutionFaceAndProduct sp : list) {
				Product p = sp.getProduct();
				productMap.put(p.getId(), p.getName());
			}
			solutionFaceProductMix.setSolutionFaceProductStep(step);
			long solutionId = step.getSolutionFace().getId();
			model.addAttribute("solutionId", solutionId);
		}
		model.addAttribute("productMap", productMap);
		model.addAttribute("solutionFaceProductMix", solutionFaceProductMix);
		CacheUtils.put("select_mixId", solutionFaceProductMix.getId());
		return "modules/expert/solutionFaceProductMixForm";
	}

	@RequestMapping(value = "save")
	public String save(SolutionFaceProductMix solutionFaceProductMix, Model model, RedirectAttributes redirectAttributes) {
		solutionFaceProductMixService.save(solutionFaceProductMix);
		long stepId= solutionFaceProductMix.getSolutionFaceProductStep().getId();
		addMessage(redirectAttributes, "保存 方案产品步骤成功");
		long id = solutionFaceProductMix.getId();
		CacheUtils.put("select_mixId", id);
		return "redirect:"+Global.ADMIN_PATH+"/expert/solutionFaceProductMix/form?stepId=" + stepId+"&id=" + id;
	}
	
	@RequestMapping(value = "delete")
	public String delete(SolutionFaceProductMix solutionFaceProductMix, RedirectAttributes redirectAttributes) {
		long stepId= solutionFaceProductMix.getSolutionFaceProductStep().getId();
		solutionFaceProductMixService.delete(solutionFaceProductMix);
		addMessage(redirectAttributes, "删除 方案产品步骤成功");
		return "redirect:"+Global.ADMIN_PATH+"/expert/solutionFaceProductMix/?stepId=" + stepId;
	}
}
